<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">











<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Math - The Commons Math User Guide - Complex Numbers</title>
    <style type="text/css" media="all">
      @import url("../css/maven-base.css");
      @import url("../css/maven-theme.css");
      @import url("../css/site.css");
    </style>
    <link rel="stylesheet" href="../css/print.css" type="text/css" media="print" />
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
      </head>
  <body class="composite">
    <div id="banner">
                    <span id="bannerLeft">
    
            Commons Math User Guide
    
            </span>
                    <div class="clear">
        <hr/>
      </div>
    </div>
    <div id="breadcrumbs">
          
  

  
    
  
  
    
              <div class="xright">      
  

  
    
  
  
    
  </div>
      <div class="clear">
        <hr/>
      </div>
    </div>
    <div id="leftColumn">
      <div id="navcolumn">
           
  

  
    
  
  
    
                   <h5>User Guide</h5>
            <ul>
              
    <li class="none">
                    <a href="../userguide/index.html">Contents</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/overview.html">Overview</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/stat.html">Statistics</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/random.html">Data Generation</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/linear.html">Linear Algebra</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/analysis.html">Numerical Analysis</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/special.html">Special Functions</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/utilities.html">Utilities</a>
          </li>
              
    <li class="none">
              <strong>Complex Numbers</strong>
        </li>
              
    <li class="none">
                    <a href="../userguide/distribution.html">Distributions</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/fraction.html">Fractions</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/transform.html">Transform Methods</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/geometry.html">3D Geometry</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/optimization.html">Optimization</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/ode.html">Ordinary Differential Equations</a>
          </li>
              
    <li class="none">
                    <a href="../userguide/genetics.html">Genetic Algorithms</a>
          </li>
          </ul>
                                           <a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
            <img alt="Built by Maven" src="../images/logos/maven-feather.png"></img>
          </a>
                       
  

  
    
  
  
    
        </div>
    </div>
    <div id="bodyColumn">
      <div id="contentBox">
        <div class="section"><h2><a name="a7_Complex_Numbers"></a>
7 Complex Numbers</h2>
<div class="section"><h3><a name="a7.1_Overview"></a>
7.1 Overview</h3>
<p>
          The complex packages provides a complex number type as well as complex
          versions of common transcendental functions and complex number
          formatting.
        </p>
</div>
<div class="section"><h3><a name="a7.2_Complex_Numbers"></a>
7.2 Complex Numbers</h3>
<p><a href="../apidocs/org/apache/commons/math/complex/Complex.html">
          org.apache.commons.math.complex.Complex</a>
 provides a complex number
          type that forms the basis for the complex functionality found in 
          commons-math.
         </p>
<p>
           Complex functions and arithmetic operations are implemented in
           commons-math by applying standard computational formulas and
           following the rules for <code>java.lang.Double</code> arithmetic in 
           handling infinite and <code>NaN</code> values.  No attempt is made
           to comply with ANSII/IEC C99x Annex G or any other standard for
           Complex arithmetic.  See the class and method javadocs for the 
           <a href="../apidocs/org/apache/commons/math/complex/Complex.html">
           Complex</a>
 and
           <a href="../apidocs/org/apache/commons/math/complex/ComplexUtils.html">
           ComplexUtils</a>
 classes for details on computing formulas.
        </p>
<p>
          To create a complex number, simply call the constructor passing in two
          floating-point arguments, the first being the real part of the
          complex number and the second being the imaginary part:
          <div class="source"><pre>Complex c = new Complex(1.0, 3.0); // 1 + 3i</pre>
</div>
</p>
<p>
          Complex numbers may also be created from polar representations
          using the <code>polar2Complex</code> method in 
          <code>ComplexUtils</code>.
        </p>
<p>
          The <code>Complex</code> class provides basic unary and binary
          complex number operations.  These operations provide the means to add,
          subtract, multiply and divide complex numbers along with other
          complex number functions similar to the real number functions found in
          <code>java.math.BigDecimal</code>:
          <div class="source"><pre>Complex lhs = new Complex(1.0, 3.0);
Complex rhs = new Complex(2.0, 5.0);

Complex answer = lhs.add(rhs);       // add two complex numbers
        answer = lhs.subtract(rhs);  // subtract two complex numbers
        answer = lhs.abs();          // absolute value
        answer = lhs.conjugate(rhs); // complex conjugate</pre>
</div>
</p>
</div>
<div class="section"><h3><a name="a7.3_Complex_Transcendental_Functions"></a>
7.3 Complex Transcendental Functions</h3>
<p><a href="../apidocs/org/apache/commons/math/complex/Complex.html">
          org.apache.commons.math.complex.Complex</a>
 also provides
          implementations of serveral transcendental functions involving complex
          number arguments.  Prior to version 1.2, these functions were provided
          by <a href="../apidocs/org/apache/commons/math/complex/ComplexUtils.html">
          org.apache.commons.math.complex.ComplexUtils</a>
 in a way similar to the real
          number functions found in <code>java.lang.Math</code>, but this has been
          deprecated.  These operations provide the means to compute the log, sine,
          tangent, and other complex values :
          <div class="source"><pre>Complex first  = new Complex(1.0, 3.0);
Complex second = new Complex(2.0, 5.0);

Complex answer = first.log();        // natural logarithm.
        answer = first.cos();        // cosine
        answer = first.pow(second);  // first raised to the power of second</pre>
</div>
</p>
</div>
<div class="section"><h3><a name="a7.4_Complex_Formatting_and_Parsing"></a>
7.4 Complex Formatting and Parsing</h3>
<p><code>Complex</code> instances can be converted to and from strings
          using the<a href="../apidocs/org/apache/commons/math/complex/ComplexFormat.html">
          org.apache.commons.math.complex.ComplexFormat</a>
 class.
          <code>ComplexFormat</code> is a <code>java.text.Format</code>
          extension and, as such, is used like other formatting objects (e.g.
          <code>java.text.SimpleDateFormat</code>):
          <div class="source"><pre>ComplexFormat format = new ComplexFormat(); // default format
Complex c = new Complex(1.1111, 2.2222);
String s = format.format(c); // s contains &quot;1.11 + 2.22i&quot;</pre>
</div>
</p>
<p>
          To customize the formatting output, one or two
          <code>java.text.NumberFormat</code> instances can be used to construct
          a <code>ComplexFormat</code>.  These number formats control the
          formatting of the real and imaginary values of the complex number:
          <div class="source"><pre>NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(3);
nf.setMaximumFractionDigits(3);

// create complex format with custom number format
// when one number format is used, both real and
// imaginary parts are formatted the same
ComplexFormat cf = new ComplexFormat(nf);
Complex c = new Complex(1.11, 2.2222);
String s = format.format(c); // s contains &quot;1.110 + 2.222i&quot;

NumberFormat nf2 = NumberFormat.getInstance();
nf.setMinimumFractionDigits(1);
nf.setMaximumFractionDigits(1);

// create complex format with custom number formats
cf = new ComplexFormat(nf, nf2);
s = format.format(c); // s contains &quot;1.110 + 2.2i&quot;</pre>
</div>
</p>
<p>
          Another formatting customization provided by
          <code>ComplexFormat</code> is the text used for the imaginary
          designation.  By default, the imaginary notation is &quot;i&quot; but, it can be
          manipulated using the <code>setImaginaryCharacter</code> method.
        </p>
<p>
          Formatting inverse operation, parsing, can also be performed by
          <code>ComplexFormat</code>.  Parse a complex number from a string,
          simply call the <code>parse</code> method:
          <div class="source"><pre>ComplexFormat cf = new ComplexFormat();
Complex c = cf.parse(&quot;1.110 + 2.222i&quot;);</pre>
</div>
</p>
</div>
</div>

      </div>
    </div>
    <div class="clear">
      <hr/>
    </div>
    <div id="footer">
      <div class="xright">&#169;  
          2003-2009
    
          
  

  
    
  
  
    
  </div>
      <div class="clear">
        <hr/>
      </div>
    </div>
  </body>
</html>
